package com.example.spingsercurity2.util;

import java.io.IOException;
import java.util.HashMap;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

/**
 自定义认证失败处理类
 **/
public class MyAuthExcetion {

	/**
	 * token认证失败
	 */
	@Component
	public static class MyAuthenticationEntry implements AuthenticationEntryPoint {
		@Override
		public void commence( HttpServletRequest request,
			HttpServletResponse response,
			AuthenticationException authException) throws IOException {
			//token认证失败
			HashMap map = new HashMap();
			map.put("code", 402);
			map.put("msg", "token is invalid");
			response.getWriter().println(map.toString());
		}
	}

	/**
	 * 角色权限认证失败
	 */
	@Component
	public static class MyAccessDenied implements AccessDeniedHandler {
		@Override
		public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException e) throws IOException, ServletException {
			//角色权限认证失败
			HashMap map = new HashMap();
			map.put("code", 401);
			map.put("msg", "accessDenied");
			response.getWriter().println(map.toString());
		}
	}

}


